package com.example.hystrix;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

public class ObservableCommandHelloWorld extends HystrixObservableCommand<String> {

    private String name;

    protected ObservableCommandHelloWorld(String name) {
        super(HystrixCommandGroupKey.Factory.asKey("exampleGroup"));
        this.name=name;
    }

    @Override
    protected Observable<String> construct() {

        //int i=1/0   调用熔断

        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                System.out.println("1.1 "+Thread.currentThread().getName());
                subscriber.onNext("hello");
                subscriber.onNext(name);
                System.out.println("1.2 "+Thread.currentThread().getName());
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io());   //subscribeOn 解读https://blog.csdn.net/coobee/article/details/105932347, https://blog.csdn.net/ScottePerk/article/details/124695651
    }

    @Override
    protected Observable<String> resumeWithFallback() {
        return Observable.just("熔断");
    }
}
